﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace lcs.Web.demandmaterialinfo
{
    public partial class Add : Web.Admin.BasePage
    {
        public string type = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "" && Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
                {
                    type = Request.QueryString["type"].ToString();
                    //if (type != "0")
                    //{
                    //    txtestimate.Enabled = false;
                    //    //txtprice.Enabled = false;
                    //}
                    //else
                    //{
                    //    txtestimate.Enabled = true;
                    //    txtprice.Enabled = true;

                    //}
                }
                Page.DataBind();
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "" && Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
            {
                int dpid = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out dpid);
                type = Request.QueryString["type"].ToString();
                string strErr = "";
                if (this.hfmbm.Value.Trim().Length == 0)
                {
                    strErr += "物料编码不能为空！\\n";
                }
                //if (this.txtvtype.Text.Trim().Length == 0)
                //{
                //    strErr += "电压等级千伏不能为空！\\n";
                //}
                //if (!PageValidate.IsDecimal(txtnum.Text))
                //{
                //    strErr += "需用数量格式错误！\\n";
                //}
                if (!PageValidate.IsDateTime(txtdeliverytime.Text))
                {
                    strErr += "交货时间格式错误！\\n";
                }
                if (this.txtdeliveryadd.Text.Trim().Length == 0)
                {
                    strErr += "交货地点不能为空！\\n";
                }                
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                string mbm = this.hfmbm.Value;
                string vtype = this.txtvtype.Text;
                decimal num = decimal.Parse(this.txtnum.Text);
                DateTime deliverytime = DateTime.Parse(this.txtdeliverytime.Text);
                string deliveryadd = this.txtdeliveryadd.Text;
                decimal estimate = 0;
                decimal price = 0;
                //if (type != "0")
                //{
                //    price = decimal.Parse(hfprice.Value);
                //    estimate = price * num / 10000;
                //    //string ssextimate = txtestimate.Text;
                //    //estimate = decimal.Parse(txtestimate.Text);//要求可以自己修改
                    
                //}
                //else
                //{
                    price = decimal.Parse(txtprice.Text);
                    estimate = decimal.Parse(txtestimate.Text);
                //}
                   
                lcs.Model.demandmaterialinfo model = new lcs.Model.demandmaterialinfo();
                model.mbm = mbm;
                model.vtype = vtype;
                model.num = num;
                model.deliverytime = deliverytime;
                model.deliveryadd = deliveryadd;
                model.estimate = estimate;
                model.price = price;
                model.dpid = dpid;
                lcs.BLL.demandmaterialinfo bll = new lcs.BLL.demandmaterialinfo();
                if (bll.Add(model) > 0)
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "List.aspx?id=" + dpid + "&type=" + type);
                else
                    Maticsoft.Common.MessageBox.Show(this, "增加失败，同一物料不能增加多个！");
            }
        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "" && Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
            {
                int dpid = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out dpid);
                type = Request.QueryString["type"].ToString();
                Response.Redirect("List.aspx?id=" + dpid + "&type=" + type);
            }
        }
    }
}
